
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author tien
 */
public class PlayingData extends BaseRMS{
    private int mapType = Level.Lake;
    
    public int getMapType() {
        return this.mapType;
    }
    
    public void setMapType(int type) {
        this.mapType = type;
    }
    
    private int nmbSpider = 0;
    
    public int getNmbSpider() {
        return this.nmbSpider;
    }
    
    public void setNmbSpider(int nmb) {
        this.nmbSpider = nmb;
    }
    
    private Vector listSpiderMovingData;
    
    MovingData LoadSpiderMovingData(int i) {
        return (MovingData)listSpiderMovingData.elementAt(i);
    }
    
    public void setSpiderMovingData(Vector listMovingData) {
        this.listSpiderMovingData = listMovingData;
    }
    
    private int nmbBullet = 0;
    
    public int getNmbBullet() {
        return this.nmbBullet;
    }
    
    public void setNmbBullet(int nmb) {
        this.nmbSpider = nmb;
    }
    
    private Vector listBulletMovingData;
    
    MovingData LoadBulletMovingData(int i) {
        return (MovingData)listBulletMovingData.elementAt(i);
    }
    
    public void setBulletMovingData(Vector listMovingData) {
        this.listBulletMovingData = listMovingData;
    }
    
    private int spawnDelayTime = 0;
    
    public int getSpawnDelayTime() {
        return this.spawnDelayTime;
    }
    
    public void setSpawnDelayTime(int time) {
        this.spawnDelayTime = time;
    }
    
    private boolean haveBoss = false;
    
    public boolean isHaveBoss() {
        return this.haveBoss;
    }
    
    public void setHasBoss(boolean hasBoss) {
        this.haveBoss = hasBoss;
    }
    
    public String getRMSName() {
      return "MushroomGamePlayingData";
  }
    
    void loadData() throws Exception {
        try {
            byte[] record = this.getRecordStore().getRecord(1);
            DataInputStream istream = new DataInputStream(new ByteArrayInputStream(record,0,record.length));
            mapType = istream.readInt();
            nmbSpider = istream.readInt();
            
            for (int i = 0; i < this.nmbSpider; i++) {
                MovingData spiderMovingData = new MovingData(new Point(istream.readInt(), istream.readInt()), istream.readInt(), istream.readInt());
                this.listSpiderMovingData.addElement(spiderMovingData);
            }
            
            nmbBullet = istream.readInt();
            
            for (int i = 0; i < this.nmbBullet; i++) {
                MovingData bulletMovingData = new MovingData(new Point(istream.readInt(), istream.readInt()), istream.readInt(), istream.readInt());
                this.listBulletMovingData.addElement(bulletMovingData);
            }
            
            spawnDelayTime = istream.readInt();
            haveBoss = istream.readBoolean();
        } catch (Exception e) {
          throw new Exception (this.getRMSName() + "::loadMapType::" + e);
        }
    }

    void createDefaultData() throws Exception {
        this.mapType = Level.Lake;

        try {
            ByteArrayOutputStream bstream = new ByteArrayOutputStream(12);
            DataOutputStream ostream = new DataOutputStream(bstream);
            ostream.writeInt(mapType);
            
            ostream.writeInt(nmbSpider);
            
            for (int i = 0; i < this.nmbSpider; i++) {
                ostream.writeInt(((MovingData)listSpiderMovingData.elementAt(i)).point.x);
                ostream.writeInt(((MovingData)listSpiderMovingData.elementAt(i)).point.x);
                ostream.writeInt(((MovingData)listSpiderMovingData.elementAt(i)).direction);
                ostream.writeInt(((MovingData)listSpiderMovingData.elementAt(i)).previousDirection);
            }
            
            ostream.writeInt(nmbBullet);
            
            for (int i = 0; i < this.nmbBullet; i++) {
                ostream.writeInt(((MovingData)listBulletMovingData.elementAt(i)).point.x);
                ostream.writeInt(((MovingData)listBulletMovingData.elementAt(i)).point.x);
                ostream.writeInt(((MovingData)listBulletMovingData.elementAt(i)).direction);
                ostream.writeInt(((MovingData)listBulletMovingData.elementAt(i)).previousDirection);
            }
            
            ostream.writeInt(spawnDelayTime);
            ostream.writeBoolean(haveBoss);
            ostream.flush();
            ostream.close();
            byte[] record = bstream.toByteArray();
            this.getRecordStore().addRecord(record, 0, record.length);
        } catch (Exception e) {
            throw new Exception(this.getRMSName() + "::createDefaultData::" + e);
        }
    }

    void updateData() throws Exception {
        try {
            ByteArrayOutputStream bstream = new ByteArrayOutputStream(12);
            DataOutputStream ostream = new DataOutputStream(bstream);
            ostream.writeInt(mapType);
            ostream.writeInt(nmbSpider);
            
            for (int i = 0; i < this.nmbSpider; i++) {
                ostream.writeInt(((MovingData)listSpiderMovingData.elementAt(i)).point.x);
                ostream.writeInt(((MovingData)listSpiderMovingData.elementAt(i)).point.x);
                ostream.writeInt(((MovingData)listSpiderMovingData.elementAt(i)).direction);
                ostream.writeInt(((MovingData)listSpiderMovingData.elementAt(i)).previousDirection);
            }
            
            ostream.writeInt(nmbBullet);
            
            for (int i = 0; i < this.nmbBullet; i++) {
                ostream.writeInt(((MovingData)listBulletMovingData.elementAt(i)).point.x);
                ostream.writeInt(((MovingData)listBulletMovingData.elementAt(i)).point.x);
                ostream.writeInt(((MovingData)listBulletMovingData.elementAt(i)).direction);
                ostream.writeInt(((MovingData)listBulletMovingData.elementAt(i)).previousDirection);
            }
            
            ostream.writeInt(spawnDelayTime);
            ostream.writeBoolean(haveBoss);
            ostream.flush();
            ostream.close();
            byte[] record = bstream.toByteArray();
            this.getRecordStore().setRecord(1, record, 0, record.length);
        } catch (Exception e) {
            throw new Exception(this.getRMSName() + "::createDefaultData::" + e);
        }
    }

    void loadPlayingData() throws Exception {
        try {
            // Will call either loadData() or createDefaultData()
            this.open();

            if (getNumRecords() > 0) {
                loadData();
            } else {
                createDefaultData();
            }

            // Close
            if (this.getRecordStore() != null)
                this.close();
            } catch (Exception e) {
            throw new Exception("Error load playing data" + e);
        }
    }
    
}
